import auditService from "@/service/common/audit.service";
import { alertC, confirmC, jsonCopy, numberC } from "@/util/commonVerify";

// 批量提交审核的接口
// moduleType、busType、busIds
export const applayV2 = (tabSelData, params) =>  {
  if(tabSelData.length == 0) {
    alertC("请选择需要上报的数据！");
    return Promise.reject()
  }

  let noFlow = tabSelData.some(i => !['1', '4'].includes(i.status + ''));
  if(noFlow) {
    alertC("只能送审数据状态为【未提交】【被退回】的数据！");
    return Promise.reject()
  }
  
  let tParams = jsonCopy(params)
  delete tParams.busId
  tParams.busIds = tabSelData.map(i => i.keyid)
  return new Promise((resolve, reject) => {
    auditService.applyV2(tParams).then(res => {
      alertC(res.data.msg)
      resolve(res)
    })
    .catch(err => reject(err))
  })
}

// 财务处理，审核通过
export const financeDeal = (params, financeData = []) =>  {
  const tform = jsonCopy(params);
  tform.busIds = [tform.busId]
  if(financeData.length != 0) {
    tform.extra.busiTPaybudgetList = financeData
  }
  return new Promise((resolve, reject) => {
    confirmC('确认处理?').then(() => {
      auditService.batchApprove(tform).then(res => {
        if(res.data.code == 0) {
          alertC(res.data.msg, "success");
          resolve(res)
        }
        else {
          alertC(res.data.msg, "warning");
          reject()
        }
      })
      .catch(() =>  reject())
    })
    .catch(err => reject())
  })
}

